package com.hd.controller;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hd.controller.base.BaseController;
import com.hd.entity.Notice;
import com.hd.entity.NoticeReaded;
import com.hd.service.NoticeReadedService;
import com.hd.service.NoticeService;
import com.hd.util.StringUtils;
import com.hd.util.TransformCharacter;
import com.hd.util.result.PageInfo;

/**
* 通知公告，最新动态
*/
@RequestMapping("notice")
@Controller
public class NoticeController extends BaseController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private NoticeReadedService noticeReadedService;
    /**
     * 主页 type=2通知公告 type=1最新动态
     * @return
     */
    @RequestMapping("manager")
    public String manager(Model model,String type) {
    	model.addAttribute("type", type);
        return "jsp/notice/noticeList";
    }
    /**
     * 列表
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping(value="/dataGrid",method=RequestMethod.POST)
    @ResponseBody
    public Object dataGrid(Notice notice, Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String,Object>condition=new HashMap<String,Object>();
        condition.put("type", notice.getType());
        if(StringUtils.isNotNull(notice.getTitle())) {
        	condition.put("title", notice.getTitle());
        }
        //消息发起人
        if(StringUtils.isNotNull(notice.getUserid())) {
        	condition.put("userid", notice.getUserid());
        }
        //当前登录用户
        condition.put("currentUserid", getUserId());
        pageInfo.setCondition(condition);
        noticeService.selectDataGrid(pageInfo);
        return pageInfo;
    }

    /**
     * 添加页
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage(Model model,String type) {
    	model.addAttribute("type", type);
        return "jsp/notice/noticeAdd";
    }

    /**
     * 添加
     * @param notice
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Object add(Notice notice) {
    	notice.setUserid(getUserId());
    	if(notice.getOvert()!=1) {
    		notice.setOvert(1);
    	}
    	notice.setUserName(getStaffName());
    	if(StringUtils.isNotNull(notice.getContent())) {
    		String content=TransformCharacter.doUntransition(notice.getContent());
    		notice.setContent(content);
    	}
        noticeService.save(notice);
        return renderSuccess("添加成功！");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@PathVariable("id") String id) {
        noticeService.delete(id);
        return renderSuccess("删除成功！");
    }
    /**
     * 编辑页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editPage/{id}")
	public String editPage(Model model, @PathVariable("id")String id) {
	 Notice notice = noticeService.get(id);
        model.addAttribute("notice", notice);
        return "jsp/notice/noticeEdit";
    }

    /**
     * 查看页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/viewPage/{id}")
    public String viewPage(Model model, @PathVariable("id") String id) {
        Notice notice = noticeService.get(id);
        Integer readed = noticeService.getReaded(notice, getUserId());
        if(readed==0){
        	NoticeReaded noticeReaded=new NoticeReaded();
        	noticeReaded.setNoticeId(id);
        	noticeReaded.setReadTime(new Date());
        	noticeReaded.setUserid(getUserId());
        	noticeReaded.setType(notice.getType());
        	noticeReadedService.save(noticeReaded);
        }
        model.addAttribute("notice", notice);
        return "jsp/notice/noticeView";
    }
    /**
     * 编辑
     * @param notice
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public Object edit(Notice notice) {
    	Notice vo=noticeService.get(notice.getId());
    	notice.setCreateTime(vo.getCreateTime());
    	if(notice.getOvert()!=1) {
    		notice.setOvert(1);
    	}
    	if(StringUtils.isNotNull(notice.getContent())) {
    		String content=TransformCharacter.doUntransition(notice.getContent());
    		notice.setContent(content);
    	}
        noticeService.update(notice);
        return renderSuccess("编辑成功！");
    }
    @ResponseBody
    @RequestMapping(value="getNewDynamic",produces= {"text/html;charset=UTF-8"})
    public Object getNewDynamic() {
    	String s=" <div id=\"myCarousel\" class=\"carousel slide\">" + 
    			"						<!-- 轮播（Carousel）指标 -->" + 
    			"						<ol class=\"carousel-indicators\">" + 
    			"							<li data-target=\"#myCarousel\" data-slide-to=\"0\" class=\"active\"></li>" + 
    			"							<li data-target=\"#myCarousel\" data-slide-to=\"1\"></li>" + 
    			"							<li data-target=\"#myCarousel\" data-slide-to=\"2\"></li>" + 
    			"						</ol>   " + 
    			"						<!-- 轮播（Carousel）项目 -->" + 
    			"						<div class=\"carousel-inner\">" + 
    			"							<div class=\"item active\">" + 
    			"								<img src=\"../static/img/ynjt_logo.png\" alt=\"First slide\" style=\"height:200px;width:100%\" onclick=\"alert(1)\"> " + 
    			"								<div class=\"carousel-caption\">标题 1</div>" + 
    			"							</div>" + 
    			"							<div class=\"item\">" + 
    			"								<img src=\"../static/img/ynjt_logo.png\" alt=\"First slide\" style=\"height:200px;width:100%\" onclick=\"alert(2)\">" + 
    			"								<div class=\"carousel-caption\">标题 2</div>" + 
    			"							</div>" + 
    			"							<div class=\"item\">" + 
    			"								<img src=\"../static/img/ynjt_logo.png\" alt=\"First slide\" style=\"height:200px;width:100%\" onclick=\"alert(3)\">" + 
    			"								<div class=\"carousel-caption\">标题 3</div>" + 
    			"							</div>" + 
    			"						</div>" + 
    			"						<!-- 轮播（Carousel）导航 -->" + 
    			"						<a class=\"left carousel-control\" href=\"#myCarousel\" role=\"button\" data-slide=\"prev\">" + 
    			"						    <span class=\"glyphicon glyphicon-chevron-left\" aria-hidden=\"true\"></span>" + 
    			"						    <span class=\"sr-only\">Previous</span>" + 
    			"						</a>" + 
    			"						<a class=\"right carousel-control\" href=\"#myCarousel\" role=\"button\" data-slide=\"next\">" + 
    			"						    <span class=\"glyphicon glyphicon-chevron-right\" aria-hidden=\"true\"></span>" + 
    			"						    <span class=\"sr-only\">Next</span>" + 
    			"						</a>" + 
    			"					</div> " + 
    			"				    <ul>" + 
    			"				    	<li>最新动态1</li>" + 
    			"				    	<li>最新动态2</li>" + 
    			"				    	<li>最新动态3</li>" + 
    			"				    </ul>";
    	return s;
    }

    /**
     * 获取当前登录人未阅读通知的总数
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getNoticeTip")
    public Object getNoticeTip(String userid,String type) {
    	Long total=noticeService.getNonReadedCount(userid,type);
    	System.out.println(total);
    	return total;
    }
}
